package com.vj.space.api.common.utils;

import cn.hutool.core.date.DateUtil;

/*******************************************************************************
 * Description: 统一结果返回工具类
 * 使用方式  成功调用ok方法  失败调用error方法
 * 1.支持链式编程 2.支持静态调用  3.多种重载
 * @author quyi
 * @version 1.0
 */
public class R {

    private boolean success = true;//成功还是失败
    private Integer code = 200;//状态码
    private String message = "";//信息
    private Object resultObj = null;//数据
    private String timestamp = DateUtil.now();//时间戳 yyyy-MM-dd HH:mm:ss
    //实例对象
    public static R INSTANCE = new R();

    //成功返回
    public R ok(){
        return this;
    }

    public R ok(Object resultObj){
        this.resultObj = resultObj;
        return this;
    }

    //失败返回
    public R error(String message){
        this.success = false;
        this.code = 500;//默认错误码500
        this.message = message;
        return this;
    }

    public R error(Integer code,String message){
        this.success = false;
        this.code = code;
        this.message = message;
        return this;
    }

    /*get set 方法  set支持链式编程*/
    public boolean isSuccess() {
        return success;
    }

    public R setSuccess(boolean success) {
        this.success = success;
        return this;
    }

    public Integer getCode() {
        return code;
    }

    public R setCode(Integer code) {
        this.code = code;
        return this;
    }

    public String getMessage() {
        return message;
    }

    public R setMessage(String message) {
        this.message = message;
        return this;
    }

    public Object getResultObj() {
        return resultObj;
    }

    public R setResultObj(Object resultObj) {
        this.resultObj = resultObj;
        return this;
    }

    @Override
    public String toString() {
        return "R{" +
                "success=" + success +
                ", code=" + code +
                ", message='" + message + '\'' +
                ", resultObj=" + resultObj +
                ", timestamp='" + timestamp + '\'' +
                '}';
    }
}

